package imdbfolder.gui;

import imdbfolder.app.Application;
import imdbfolder.data.SortMode;
import imdbfolder.gui.icons.IconStore;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class ImdbFolderWindow extends JFrame {

    private static final long serialVersionUID = 1L;

    private ImdbFolderController controller;

    private JTextField folderTextField;

    private JButton selectFolderButton;

    //private JLabel statusLabel;

    private JButton cancelButton;

    private JTextField searchField;

    private JComboBox<SortMode> sortingComboBox;

    public ImdbFolderWindow(ImdbFolderController imdbFolderController) {
        controller = imdbFolderController;

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void initialize() {
        setTitle("ImdbFolder " + Application.VERSION);

        getContentPane().setLayout(new MigLayout("fill, nogrid, w :1100:, h 600"));

        setWindowIcon();

        addComponents();
        observeComponents();

        pack();
        centerWindow();
    }

    private void setWindowIcon() {
        setIconImage(IconStore.getImage("imdb.png"));
    }

    private void centerWindow() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        int x = (screenSize.width - getWidth()) / 2;
        int y = (screenSize.height - getHeight()) / 2;

        setLocation(x, y);
    }

    private void observeComponents() {
        selectFolderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.browseForFolder();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.cancelAllTasks();
            }
        });

        searchField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (!searchField.getText().equals("search")) {
                    controller.searchText(searchField.getText());
                }
            }
        });

        searchField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                searchField.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (searchField.getText().equals("")) {
                    controller.searchText("");
                    searchField.setText("Search..");
                }
            }
        });

        sortingComboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                controller.setSortMode((SortMode) sortingComboBox.getSelectedItem());
            }

        });
        
        folderTextField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File file = new File(folderTextField.getText());

                if (file.exists() && file.isDirectory()) {
                    controller.setFolderSelection(file);
                    controller.loadSelectedFolder();
                }
            }
        });
    }

    private void addComponents() {
        getContentPane().add(new JLabel("Folder:"));

        folderTextField = new JTextField();
        getContentPane().add(folderTextField, "growx");

        selectFolderButton = new JButton("Browse..");
        getContentPane().add(selectFolderButton, "wmin button");

        sortingComboBox = new JComboBox<SortMode>();
        for (SortMode mode : SortMode.values()) {
            sortingComboBox.addItem(mode);
        }
        getContentPane().add(sortingComboBox);

        searchField = new JTextField("Search..");
        add(searchField, "w 150lp");

        cancelButton = new JButton("Cancel", IconStore.get("cancel.png"));
        add(cancelButton, "wrap");
        
        getContentPane().add(controller.getMovieListController().getView(), "newline, h 500, grow, spanx, wrap");

        /*
        statusLabel = new JLabel("Application Started");
        statusLabel.setBorder(BorderFactory.createEtchedBorder());
        add(statusLabel, "growx, h 25");
        */

    }

    public void onFolderSelected(File selectedFile) {
        folderTextField.setText(selectedFile.getAbsolutePath());
    }

    public void setStatus(String status) {
        //statusLabel.setText(status);
    }

}
